﻿using System.Linq;
using Stimulus360.Models.Cms;
using Stimulus360.Repositories.Interfaces;

namespace Stimulus360.Repositories
{
    /// <summary>
    ///     This class is reponsible for interaction with the CMS DB which stores
    ///     Project Photos and documents.
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable",Justification="DataContext does not hold expensive resources. Will be garbage collected automatically.")]
    public class CmsRepository : ICmsRepository
    {

        #region ICMSRepository Members

        /// <summary>
        ///     The Database context for interaction with the CMS DB
        /// </summary>
        private readonly CmsDataContext _dbContext = new CmsDataContext();

        /// <summary>
        ///     Method to add a project photo
        /// </summary>
        /// <param name="photo">
        ///     The Photo to be added
        /// </param>
        public void AddPhoto(ProjectPhoto photo)
        {
            _dbContext.ProjectPhotos.InsertOnSubmit(photo);
            _dbContext.SubmitChanges();
        }

        /// <summary>
        ///     Method to retrieve all the photos for the given project
        /// </summary>
        /// <param name="projectId">
        ///     Project for which photos need to be retrieved
        /// </param>
        /// <returns>
        ///     List of photos
        /// </returns>
        public IQueryable<ProjectPhoto> GetPhotos(int projectId)
        {
            return from photo in _dbContext.ProjectPhotos
                   where photo.ProjectId == projectId
                   orderby photo.DateUploaded descending
                   select photo;

        }

        /// <summary>
        ///     Method to get the specified photo 
        /// </summary>
        /// <param name="photoId">
        ///     The photo to be retrieved
        /// </param>
        /// <returns>
        ///     Photo corresponding to the photoId
        /// </returns>
        public ProjectPhoto FindPhotoById(int photoId)
        {
            return _dbContext.ProjectPhotos.FirstOrDefault(p => p.PhotoId == photoId);
        }

        /// <summary>
        ///     Method to add a document to a project
        /// </summary>
        /// <param name="document">
        ///     The document to be added
        /// </param>
        public void AddDocument(ProjectDocument document)
        {
            _dbContext.ProjectDocuments.InsertOnSubmit(document);
            _dbContext.SubmitChanges();
        }

        /// <summary>
        ///     Method to retrieve all documents for the given project
        /// </summary>
        /// <param name="projectId">
        ///     Project for which documents are to be retrieved
        /// </param>
        /// <returns>
        ///     List of project documents.
        /// </returns>
        public IQueryable<ProjectDocument> GetDocuments(int projectId)
        {
            return from doc in _dbContext.ProjectDocuments
                   where doc.ProjectId == projectId
                   orderby doc.DateUploaded descending
                   select doc;

        }

        /// <summary>
        ///     Method to get a specific document 
        /// </summary>
        /// <param name="documentId">
        ///     Document to be retrieved
        /// </param>
        /// <returns>
        ///     Document corresponding to the given Document ID
        /// </returns>
        public ProjectDocument FindDocumentById(int documentId)
        {
            return _dbContext.ProjectDocuments.FirstOrDefault(d => d.DocumentId == documentId);
        }

        #endregion
    }
}
